package com.Servlet;

import com.dao.DaoFactory;
import com.entity.search;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Array;
import java.sql.SQLException;
import java.util.*;

public class searchServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setHeader("Cache-control", "no-cache");
        request.setCharacterEncoding("UTF-8");
//        System.out.println("-------------------------------");
        String sc = request.getParameter("search");
        try {
            String patt = ".*"+sc+".*";

            List<search> bookall= DaoFactory.getInstance().getsearchDao().findall();
            List<search> book  = new ArrayList<search>();
            List<search> author = new ArrayList<search>();

//            System.out.println(bookall);
            for(int i = 0; i < bookall.size() ; i++){
                if(bookall.get(i).getBookname().matches(patt))
                {
//                    System.out.println("***"+bookall.get(i).getBookname());
                    book.add(bookall.get(i));
                }
                if(bookall.get(i).getAuthor_name().matches(patt))
                {
                        author.add(bookall.get(i));
                }
//                System.out.println("Bookname :"+bookall.get(i).getBookname());
//                System.out.println("Author_name :"+bookall.get(i).getAuthor_name());
//                System.out.println("Author_phone :"+bookall.get(i).getAuthor_phone());
            }
//            System.out.println("******************************");
//            System.out.println(book.size());
//            for(int i = 0 ; i < book.size() ; i++){
//
//                System.out.println("Bookname :"+book.get(i).getBookname());
//                System.out.println("Author_name :"+book.get(i).getAuthor_name());
//                System.out.println("Author_phone :"+book.get(i).getAuthor_phone());
//            }
//            System.out.println("******************************");
//            System.out.println(author.size());
//            for(int i = 0 ; i < author.size() ; i++){
//
//                System.out.println("Bookname :"+author.get(i).getBookname());
//                System.out.println("Author_name :"+author.get(i).getAuthor_name());
//                System.out.println("Author_phone :"+author.get(i).getAuthor_phone());
//            }


            request.setAttribute("findauthor",author);
            request.setAttribute("findbook",book);
            request.setAttribute("authornum",author.size());
            request.setAttribute("booknum",book.size());

        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.getRequestDispatcher("search.jsp").forward(request,response);
//        System.out.println("-------------------------------");


    }

}
